package com.glory.huaweipda.view

import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import com.glory.huaweipda.R
import com.glory.huaweipda.base.Constant
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.databinding.ActivityHomeBinding
import com.glory.huaweipda.viewmodel.HomeViewModel
import com.glory.mvvmnet.base.BaseActivity
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_DownloadFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_VersionEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils

class HomeActivity : BaseActivity<HomeViewModel>() {
    override fun layoutId(): Int {
        return R.layout.activity_home
    }

    override fun initView(savedInstanceState: Bundle?) {


        val binding: ActivityHomeBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_home)
        binding.homeViewModel = viewModel
        viewModel.initAssets(this)
        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                viewModel.START_ACTIVITY -> {
                    val intent = Intent()

                    try {
                        val entity = it.item as FreeUI_TabIndexGridEntity

                        if (entity.picName.equals("materiel_down")){
                            val area = FreeApi_SharePreferencesUtils.getSharePre(
                                "SystemSet",
                                "area",
                                applicationContext)

                            if (area.equals("")){
                                Toast.makeText(this, "请先去系统设置页面设置地址", Toast.LENGTH_SHORT).show()
                            }else{
                                val clazz = Class.forName(entity.skipClass)
                                intent.putExtra("showPlugin", entity.showPlugin)
                                intent.setClass(this, clazz)
                                startActivity(intent)
                            }
                        }else{
                            val clazz = Class.forName(entity.skipClass)
                            intent.putExtra("showPlugin", entity.showPlugin)
                            intent.setClass(this, clazz)
                            startActivity(intent)
                        }

                    } catch (var4: Exception) {
                        var4.printStackTrace()
                    }
                }
                2 ->{
                    val manager: PackageManager = this.packageManager
                    var info: PackageInfo? = null
                    try {
                        info = manager.getPackageInfo(this.packageName, 0)
                    } catch (e: PackageManager.NameNotFoundException) {
                        e.printStackTrace()
                    }
                    val version = info!!.versionName

                    val freeuiVersionentity = it.item as FreeUI_VersionEntity
                    freeuiVersionentity.currentVersion = version
                    FreeApi_SharePreferencesUtils.setSharePre("SystemSet", "FILE_URL_DEBUG", freeuiVersionentity.downUrl, MyApp.app)
                    Constant.FILE_URL = freeuiVersionentity.downUrl
                    val code = FreeApi_Utils.compareVersion(version, freeuiVersionentity.version)
                    if (code == -1) {
                        val downloadFragmentDialog = FreeUI_DownloadFragmentDialog()
                        downloadFragmentDialog.setData(freeuiVersionentity)
                        downloadFragmentDialog.show(this.supportFragmentManager, "DOWNLOAD")
                    }
                }
            }
            if ("com.glory.huaweipda.view.SkipDialogActivity".equals(getTopActivity(applicationContext))){
                SkipDialogActivity.getActivity()
            }
        }
    }

    override fun initData() {
        getTopActivity(applicationContext)
        Handler().postDelayed({
            initData()
        },3*1000)
    }


    fun getTopActivity(context: Context): String {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val cn = am.getRunningTasks(1)[0].topActivity
        Log.d("Chunna.zheng", "cls:" + cn!!.className) //包名加类名
        return cn.className
    }

    override fun onPause() {
        super.onPause()
        viewModel.initUser(this)
    }

}